package cn.broadsense.biz.handler;

import cn.broadsense.biz.dto.Channel3dDataDto;
import cn.broadsense.biz.vo.ChannelGovernanceVo;
import cn.broadsense.common.utils.StringUtils;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author huangjinshi
 * @description
 * @createDate 2023-09-12 16:16:59
 */
@Slf4j
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes({ChannelGovernanceVo.class})
public class ChannelGovernanceHandler implements TypeHandler<ChannelGovernanceVo> {


    @Override
    public void setParameter(PreparedStatement ps, int i, ChannelGovernanceVo parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSONObject.toJSONString(parameter));
    }

    @Override
    public ChannelGovernanceVo getResult(ResultSet resultSet, String s) throws SQLException {
        String result = resultSet.getString(s);
        ChannelGovernanceVo channel3dDataDto = null;
        if (StringUtils.isNoneBlank(result)) {
            channel3dDataDto = JSONObject.parseObject(result, ChannelGovernanceVo.class);
        }
        return channel3dDataDto;
    }

    @Override
    public ChannelGovernanceVo getResult(ResultSet resultSet, int i) throws SQLException {
        String result = resultSet.getString(i);
        ChannelGovernanceVo channel3dData = null;
        if (StringUtils.isNoneBlank(result)) {
            channel3dData = JSONObject.parseObject(result, ChannelGovernanceVo.class);
        }
        return channel3dData;
    }

    @Override
    public ChannelGovernanceVo getResult(CallableStatement callableStatement, int i) throws SQLException {
        String result = callableStatement.getString(i);
        ChannelGovernanceVo channel3dData = null;
        if (StringUtils.isNoneBlank(result)) {
            channel3dData = JSONObject.parseObject(result, ChannelGovernanceVo.class);
        }
        return channel3dData;
    }
}
